package com.leetcode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 49. 字母异位词分组
 * 字符及字符数 + map
 *
 * @author fy
 * @date 2022/4/8 16:37
 */
public class Solution49_1 {

    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>(strs.length);
        for (String str : strs) {
            int[] count = new int[26];
            for (int i = 0; i < str.length(); i++) {
                count[str.charAt(i) - 'a']++;
            }

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < count.length; i++) {
                if (count[i] > 0) {
                    sb.append(i + 'a').append(count[i]);
                }
            }
            String key = sb.toString();

            List<String> list = map.getOrDefault(key, new ArrayList<>());
            list.add(str);
            map.put(key, list);
        }
        return new ArrayList<>(map.values());
    }

    public static void main(String[] args) {
//        String[] strs = new String[]{""};
        String[] strs = new String[]{"a"};
        List<List<String>> res = new Solution49().groupAnagrams(strs);
        System.out.println(res);
    }

}
